package com.cms.controller.admin;

import com.cms.domain.Feedback;
import com.cms.entity.Tag;
import com.cms.service.TagService;
import org.apache.commons.lang.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;


/**
 * Controller - 标签
 * 
 * 
 * 
 */
@Controller("adminTagController")
@RequestMapping("/admin/tag")
public class TagController extends BaseController {

	@Autowired
	private TagService tagService;
	
	/**
	 * 列表
	 */
	@RequestMapping
	public String index(String name,Integer pageNumber, ModelMap modelMap) {
		setListQuery();
		if(pageNumber==null){
			pageNumber = 1;
		}
		modelMap.addAttribute("page", tagService.findPage(pageNumber,PAGE_SIZE,getCurrentSite().getId()));
		modelMap.addAttribute("name", name);
		return getView("tag/index");
	}


	/**
	 * 添加
	 */
	@RequestMapping("/add")
	public String add() {
		return getView("tag/add");
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public String save(Tag tag) {
		tag.setSiteId(getCurrentSite().getId());
		tag.setCreateDate(LocalDateTime.now());
		tag.setUpdateDate(LocalDateTime.now());
		tagService.save(tag);
		return "redirect:"+getListQuery("/admin/tag");
	}

	/**
	 * 编辑
	 */
	@RequestMapping("/edit")
	public String edit(Integer id,ModelMap modelMap) {
		modelMap.addAttribute("tag", tagService.getById(id));
		return getView("tag/edit");
	}

	/**
	 * 更新
	 */
	@RequestMapping("/update")
	public String update(Tag tag) {
		tag.setUpdateDate(LocalDateTime.now());
		tagService.updateById(tag);
		return "redirect:"+getListQuery("/admin/tag");
	}
	
	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping("/delete")
	public Feedback delete(Integer[] ids) {
		if(ArrayUtils.isNotEmpty(ids)){
			tagService.removeByIds(Arrays.asList(ids));
		}
		return Feedback.success(new HashMap<>());
	}

}